﻿define(["dojo/_base/declare", "esri/layers/tiled"], function (declare) {
    return declare(esri.layers.TiledMapServiceLayer, {

        baseURL:"",
        layerId:"",
        tileMatrixSetId: "",
        mapStyle: "",

        serviceMode: "KVP",
        imageFormat: "tiles",

        constructor: function (MapStyle) {
            this.mapStyle = MapStyle;
            this.spatialReference = new esri.SpatialReference({ wkid: 4490 });
            this.initialExtent = (this.fullExtent = new esri.geometry.Extent(-180.0, -90.0, 180.0, 90.0, this.spatialReference));

            this.tileInfo = new esri.layers.TileInfo({
                "rows": 256,
                "cols": 256,
                "compressionQuality": 0,
                "origin": {
                    "x": -180,
                    "y": 90
                },
                "spatialReference": {
                    "wkid": 4490
                },
                "lods": [
                  //{ "level": 2, "resolution": 0.3515625, "scale": 147748796.52937502 },
                  //{ "level": 3, "resolution": 0.17578125, "scale": 73874398.264687508 },
                  //{ "level": 4, "resolution": 0.087890625, "scale": 36937199.132343754 },
                  //{ "level": 5, "resolution": 0.0439453125, "scale": 18468599.566171877 },
                  //{ "level": 6, "resolution": 0.02197265625, "scale": 9234299.7830859385 },
                  //{ "level": 7, "resolution": 0.010986328125, "scale": 4617149.8915429693 },
                  //{ "level": 8, "resolution": 0.0054931640625, "scale": 2308574.9457714846 },
                  //{ "level": 9, "resolution": 0.00274658203125, "scale": 1154287.4728857423 },
                  //{ "level": 10, "resolution": 0.001373291015625, "scale": 577143.73644287116 },
                  //{ "level": 11, "resolution": 0.0006866455078125, "scale": 288571.86822143558 },
                  //{ "level": 12, "resolution": 0.00034332275390625, "scale": 144285.93411071779 },
                  //{ "level": 13, "resolution": 0.000171661376953125, "scale": 72142.967055358895 },
                  //{ "level": 14, "resolution": 8.58306884765625e-005, "scale": 36071.483527679447 },
                  //{ "level": 15, "resolution": 4.291534423828125e-005, "scale": 18035.741763839724 },
                  //{ "level": 16, "resolution": 2.1457672119140625e-005, "scale": 9017.8708819198619 },
                  //{ "level": 17, "resolution": 1.0728836059570313e-005, "scale": 4508.9354409599309 },
                  //{ "level": 18, "resolution": 5.3644180297851563e-006, "scale": 2254.4677204799655 }
                  { "level": 0, "resolution": 1.4062500000002376, "scale": 590995186.11759996 },
				{ "level": 1, "resolution": 0.703125000000119, "scale": 295497593.0588 },
                { "level": 2, "resolution": 0.351562500000059, "scale": 147748796.5294 },
                { "level": 3, "resolution": 0.17578125000003, "scale": 73874398.2647 },
                { "level": 4, "resolution": 0.0878906250000148, "scale": 36937199.1323 },
                { "level": 5, "resolution": 0.0439453125000074, "scale": 18468599.566175 },
                { "level": 6, "resolution": 0.0219726562500037, "scale": 9234299.7830875 },
                { "level": 7, "resolution": 0.0109863281250019, "scale": 4617149.89154375 },
                { "level": 8, "resolution": 0.00549316406250093, "scale": 2308574.94577187 },
                { "level": 9, "resolution": 0.00274658203125046, "scale": 1154287.47288594 },
                { "level": 10, "resolution": 0.00137329101562523, "scale": 577143.736442969 },
                { "level": 11, "resolution": 0.000686645507812616, "scale": 288571.868221484 },
                { "level": 12, "resolution": 0.000343322753906308, "scale": 144285.934110742 },
                { "level": 13, "resolution": 0.000171661376953154, "scale": 72223.960000000006 },
                { "level": 14, "resolution": 8.5830688476577E-05, "scale": 36071.4835276855 },
                { "level": 15, "resolution": 4.29153442382885E-05, "scale": 18035.7417638428 },
				{ "level": 16, "resolution": 2.14576721191443E-05, "scale": 9017.87088192139 },
				{ "level": 17, "resolution": 1.07288360595721E-05, "scale": 4508.93544096069 },
				{ "level": 18, "resolution": 5.36441802978606E-06, "scale": 2254.46772048035 },
				{ "level": 19, "resolution": 2.68220901489303E-06, "scale": 1127.23386024017 },
				{ "level": 20, "resolution": 1.34110450744652E-06, "scale": 563.616930120087 }
                ]
            });

            if (this.mapStyle == "ImageBaseMap")//获取影像地图（底图）
            {
                this.baseURL = "http://t0.tianditu.com/img_c/wmts";
                this.layerId = "img";
                this.tileMatrixSetId = "c";
            }
            else if (this.mapStyle == "ImageCNNote")//获取影像地图（中文注记）
            {
                this.baseURL = "http://t0.tianditu.com/cia_c/wmts";
                this.layerId = "cia";
                this.tileMatrixSetId = "c";
            }
            else if (this.mapStyle == "ImageENNote")//获取影像地图（英文注记）
            {
                this.baseURL = "http://t0.tianditu.com/eia_c/wmts";
                this.layerId = "eia";
                this.tileMatrixSetId = "c";
            }
            else if (this.mapStyle == "TerrainBaseMap")//获取地形图（底图）
            {
                this.baseURL = "http://t0.tianditu.com/ter_c/wmts";
                this.layerId = "ter";
                this.tileMatrixSetId = "c";
            }
            else if (this.mapStyle == "TerrainCNNote")//获取地形图（中文注记）
            {
                this.baseURL = "http://t0.tianditu.com/cta_c/wmts";
                this.layerId = "cta";
                this.tileMatrixSetId = "c";
            }
            else if (this.mapStyle == "TerrainENNote")//获取地形图（英文注记）
            {
                //暂无
            }
            else if (this.mapStyle == "VectorCNNote")//获取矢量图（中文注记）
            {
                this.baseURL = "http://t0.tianditu.com/cva_c/wmts";
                this.layerId = "cva";
                this.tileMatrixSetId = "c";
            }
            else if (this.mapStyle == "VectorENNote")//获取矢量图（英文注记）
            {
                this.baseURL = "http://t0.tianditu.com/eva_c/wmts";
                this.layerId = "eva";
                this.tileMatrixSetId = "c";
            }
            else//(mapStyle == "VectorBaseMap")获取矢量图（底图）
            {
                this.baseURL = "http://t0.tianditu.com/vec_c/wmts";
                this.layerId = "vec";
                this.tileMatrixSetId = "c";
            }

            this.loaded = true;
            this.onLoad(this);
        },

        getTileUrl: function (level, row, col) {//, baseURL, imageFormat, serviceMode, layerId, tileMatrixSetId

            var url;
            var generalUrl =  this.baseURL+"/wmts?Service=WMTS&Request=GetTile&Version=1.0.0&Style=Default&Format="
                + this.imageFormat + "&serviceMode=" + this.serviceMode + "&layer=" +
                this.layerId + "&TileMatrixSet=" + this.tileMatrixSetId + "&TileMatrix=" + level + "&TileRow=" + row + "&TileCol=" + col;

            var SDVurl = "http://www.sdmap.gov.cn/tileservice/SDPubMap?SERVICE=WMTS&VERSION=1.0.0&REQUEST=GetTile" + "&LAYER=0" + "&STYLE=default" + "&FORMAT=image/png" + "&TILEMATRIXSET=taishannew" + "&TILEMATRIX=" + level + "&TILEROW=" + row + "&TILECOL=" + col;
            var SDAurl = "http://www.sdmap.gov.cn/tileservice/SDPubMap?SERVICE=WMTS&VERSION=1.0.0&REQUEST=GetTile" + "&LAYER=0" + "&STYLE=default" + "&FORMAT=image/png" + "&TILEMATRIXSET=taishannew" + "&TILEMATRIX=" + level + "&TILEROW=" + row + "&TILECOL=" + col;

            var JNVurl = "http://119.163.121.38/service/wmts/SLDT?service=WMTS&request=GetTile&layer=0&style=default&tileMatrixSet=sss&tileMatrix=" + level + "&tileRow=" + row + "&tileCol=" + col + "&format=image/png&userName=test123&password=test123";

            var JNAurl = "http://119.163.121.38/service/wmts/SLZJ?service=WMTS&request=GetTile&layer=0&style=default&tileMatrixSet=sss&tileMatrix=" + level + "&tileRow=" + row + "&tileCol=" + col + "&format=image/png&userName=test123&password=test123";

            if (this.mapStyle == "VectorCNNote" || this.mapStyle == "VectorBaseMap") {
                if (level < 12) {
                    url = generalUrl;
                }
                if (level > 17) {
                    url = this.mapStyle == "VectorCNNote"?JNAurl:JNVurl;
                }
                if ((level > 11) && (level < 18)) {
                    url = this.mapStyle == "VectorCNNote"?SDAurl:SDVurl;
                }
            } else {
                url = generalUrl;
            }
            
            return url;
            
        }
    });
});